<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/18
 * Time: 17:36
 */

namespace app\admin\model;
use think\Model;
use think\Db;
class LoanModel extends Model{
    // 确定链接表名
    protected $table = 'snake_loan';
    public function getAll($map){
        return $this->where($map)->count();
    }

    public function getByWhere($map,$offset,$limit){
        $result=$this->where($map)->limit("$offset , $limit")->order('list_order DESC,addtime DESC,id DESC')->select();
        return $result;
    }
    public function getOne($id){
        $ret=$this->where(array('id'=>$id))->find();
        if(!empty($ret)){
            $ret['rule']=json_decode($ret['rule'],true);
            $ret['advantage']=json_decode($ret['advantage'],true);
        }
        return $ret;
    }

    public function add($param){
        $param['addtime']=time();

        $rule=$advantage=array();
        if(isset($param['rule']) && !empty($param['rule'])){
            foreach($param['rule'] as $k=>$vo){
                if($vo!='') $rule[]=$vo;
            }
        }
        if(isset($param['advantage']) && !empty($param['advantage'])){
            foreach($param['advantage'] as $k=>$vo){
                if(paramValid($vo,'tag,desc')){
                    $advantage[]=array('tag'=>$vo['tag'],'desc'=>$vo['desc']);
                }
            }
        }
        if(!empty($rule)) $param['rule']=json_encode($rule);
        if(!empty($advantage)) $param['advantage']=json_encode($advantage);
        try{
            $num=$this->save($param);
            if(false === $num){
                // 验证失败 输出错误信息
                return msg(-1, '', '提交失败');
            }else{
                return msg(1, url('loan/index'), '添加成功');
            }
        }catch(\Exception $e){
            return msg(-2, '', $e->getMessage());
        }
    }
    public function edit($param){
        $rule=$advantage=array();
        if(isset($param['rule']) && !empty($param['rule'])){
            foreach($param['rule'] as $k=>$vo){
                if($vo!='') $rule[]=$vo;
            }
        }
        if(isset($param['advantage']) && !empty($param['advantage'])){
            foreach($param['advantage'] as $k=>$vo){
                if(paramValid($vo,'tag,desc')){
                    $advantage[]=array('tag'=>$vo['tag'],'desc'=>$vo['desc']);
                }
            }
        }
        $param['rule']=json_encode($rule);
        $param['advantage']=json_encode($advantage);
        try{
            $num=$this->save($param,['id'=>$param['id']]);
            if(false === $num){
                // 验证失败 输出错误信息
                return msg(-1, '', '提交失败');
            }else{
                return msg(1, url('loan/index'), '编辑成功');
            }
        }catch(\Exception $e){
            return msg(-2, '', $e->getMessage());
        }
    }

    public function recommend($id){
        $car = self::get($id);
        $is_recommend=1;
        if($car['is_recommend']==1) $is_recommend=0;
        $car->is_recommend=$is_recommend;
        try{
            $result =  $car->save();
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{
                return msg(1, $is_recommend, '');
            }
        }catch(\Exception $e){
            return msg(-2, '', $e->getMessage());
        }
    }

    public function pass($id){
        try{
            $result =  $this->save(array('status'=>1), ['id' => $id]);
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{
                return msg(1, url('loan/index'), '审核通过');
            }
        }catch(\Exception $e){
            return msg(-2, '', $e->getMessage());
        }
    }
    public function del($id){
        try{
            $this->where('id', $id)->delete();
            return msg(1, url('loan/index'), '删除成功');
        }catch(\Exception $e){
            return msg(-2, '', $e->getMessage());
        }
    }

    public function down($id){
        try{
            $result =  $this->save(array('status'=>0), ['id' => $id]);
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{
                return msg(1, url('loan/index'), '下架成功');
            }
        }catch(\Exception $e){
            return msg(-2, '', $e->getMessage());
        }
    }

    public function up($id){
        try{
            $result =  $this->save(array('status'=>1), ['id' => $id]);
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{
                return msg(1, url('loan/index'), '上架成功');
            }
        }catch(\Exception $e){
            return msg(-2, '', $e->getMessage());
        }
    }
}